extends CharacterBody2D
class_name EnemyTank

# 敌人属性
var speed: float = 100.0
var direction: Vector2 = Vector2.DOWN
var can_shoot: bool = true
var enemy_type: int = 1  # 1-4种敌人类型
var level: int = 1       # 1-3等级
var health: int = 1      # 生命值

# 敌人类型配置
var enemy_configs = {
	1: {"score": 100, "health": 1, "speed": 100},
	2: {"score": 200, "health": 1, "speed": 120},
	3: {"score": 300, "health": 2, "speed": 80},
	4: {"score": 400, "health": 3, "speed": 90}
}

# 精灵纹理
var textures = {}

# 节点引用
@onready var sprite = $Sprite2D
@onready var collision_shape = $CollisionShape2D
@onready var shoot_timer = $ShootTimer
@onready var move_timer = $MoveTimer
@onready var direction_timer = $DirectionTimer

# 信号
signal enemy_died(score: int)
signal enemy_shot(bullet_pos: Vector2, direction: Vector2)

func _ready():
	# 随机选择敌人类型和等级
	enemy_type = randi() % 4 + 1
	level = randi() % 3 + 1
	
	# 设置敌人属性
	var config = enemy_configs[enemy_type]
	speed = config.speed
	health = config.health
	
	# 加载纹理
	load_textures()
	
	# 设置碰撞形状
	var shape = RectangleShape2D.new()
	shape.size = Vector2(50, 50)
	collision_shape.shape = shape
	
	# 连接计时器
	shoot_timer.timeout.connect(_on_shoot_timer_timeout)
	move_timer.timeout.connect(_on_move_timer_timeout)
	direction_timer.timeout.connect(_on_direction_timer_timeout)
	
	# 设置初始方向
	update_sprite()

func load_textures():
	# 根据敌人类型和等级加载纹理
	var base_path = "res://assets/Images/Enemy/EnemyTank" + str(enemy_type) + "/"
	
	textures = {
		"up": load(base_path + "EnemyTank" + str(enemy_type) + "Lv" + str(level) + "(UP).png"),
		"down": load(base_path + "EnemyTank" + str(enemy_type) + "Lv" + str(level) + "(DOWN).png"),
		"left": load(base_path + "EnemyTank" + str(enemy_type) + "Lv" + str(level) + "(LEFT).png"),
		"right": load(base_path + "EnemyTank" + str(enemy_type) + "Lv" + str(level) + "(RIGHT).png")
	}

func _physics_process(_delta):
	if not can_move():
		return
	
	move_and_slide()

func can_move() -> bool:
	return true

func _on_move_timer_timeout():
	# 随机移动
	var directions = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]
	direction = directions[randi() % directions.size()]
	velocity = direction * speed
	update_sprite()

func _on_direction_timer_timeout():
	# 随机改变方向
	var directions = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]
	direction = directions[randi() % directions.size()]
	velocity = direction * speed
	update_sprite()

func update_sprite():
	var texture_key: String
	
	match direction:
		Vector2.UP:
			texture_key = "up"
		Vector2.DOWN:
			texture_key = "down"
		Vector2.LEFT:
			texture_key = "left"
		Vector2.RIGHT:
			texture_key = "right"
	
	if texture_key in textures:
		sprite.texture = textures[texture_key]

func shoot():
	if not can_shoot:
		return
	
	can_shoot = false
	shoot_timer.start()
	
	# 计算子弹发射位置
	var bullet_pos = position + direction * 30
	
	# 发射信号
	enemy_shot.emit(bullet_pos, direction)

func _on_shoot_timer_timeout():
	can_shoot = true
	# 随机射击
	if randf() < 0.3:  # 30%概率射击
		shoot()
		# 重新开始计时器
		shoot_timer.start()

func take_damage():
	health -= 1
	if health <= 0:
		die()

func die():
	var config = enemy_configs[enemy_type]
	enemy_died.emit(config.score)
	queue_free()

func _on_area_entered(_area):
	# 处理碰撞
	pass
